package com.kexio.security.domain;

import com.baomidou.mybatisplus.annotation.*;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.kexio.core.data.domain.BaseEntity;
import com.kexio.security.domain.enums.UserStatus;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.Email;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.Pattern;
import jakarta.validation.constraints.Size;
import lombok.Data;
import lombok.EqualsAndHashCode;

import java.time.LocalDateTime;

/**
 * 用户实体
 * 
 * @author Kexio Team
 * @since 1.0.0
 */
@Data
@EqualsAndHashCode(callSuper = true)
@TableName("sys_user")
@Schema(description = "用户实体")
public class User extends BaseEntity {

    /**
     * 用户名
     */
    @NotBlank(message = "用户名不能为空")
    @Size(min = 3, max = 50, message = "用户名长度必须在3-50之间")
    @Pattern(regexp = "^[a-zA-Z0-9_]+$", message = "用户名只能包含字母、数字和下划线")
    @TableField("username")
    @Schema(description = "用户名", example = "admin")
    private String username;

    /**
     * 密码（加密）
     */
    @NotBlank(message = "密码不能为空")
    @JsonIgnore
    @TableField("password")
    @Schema(description = "密码（加密）", hidden = true)
    private String password;

    /**
     * 昵称
     */
    @Size(max = 50, message = "昵称长度不能超过50")
    @TableField("nickname")
    @Schema(description = "昵称", example = "管理员")
    private String nickname;

    /**
     * 真实姓名
     */
    @Size(max = 50, message = "真实姓名长度不能超过50")
    @TableField("real_name")
    @Schema(description = "真实姓名", example = "张三")
    private String realName;

    /**
     * 邮箱
     */
    @Email(message = "邮箱格式不正确")
    @Size(max = 100, message = "邮箱长度不能超过100")
    @TableField("email")
    @Schema(description = "邮箱", example = "admin@kexio.com")
    private String email;

    /**
     * 手机号
     */
    @Pattern(regexp = "^1[3-9]\\d{9}$", message = "手机号格式不正确")
    @TableField("phone")
    @Schema(description = "手机号", example = "13800138000")
    private String phone;

    /**
     * 头像URL
     */
    @Size(max = 500, message = "头像URL长度不能超过500")
    @TableField("avatar")
    @Schema(description = "头像URL", example = "https://example.com/avatar.jpg")
    private String avatar;

    /**
     * 状态
     */
    @TableField("status")
    @Schema(description = "用户状态", example = "1")
    private Integer status;

    /**
     * 部门ID
     */
    @TableField("dept_id")
    @Schema(description = "部门ID", example = "1")
    private Long deptId;

    /**
     * 岗位ID
     */
    @TableField("position_id")
    @Schema(description = "岗位ID", example = "1")
    private Long positionId;

    /**
     * 最后登录时间
     */
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @TableField("last_login_time")
    @Schema(description = "最后登录时间", example = "2024-01-01 12:00:00")
    private LocalDateTime lastLoginTime;

    /**
     * 最后登录IP
     */
    @Size(max = 50, message = "IP地址长度不能超过50")
    @TableField("last_login_ip")
    @Schema(description = "最后登录IP", example = "192.168.1.1")
    private String lastLoginIp;

    /**
     * 登录次数
     */
    @TableField(value = "login_count", fill = FieldFill.INSERT)
    @Schema(description = "登录次数", example = "10")
    private Integer loginCount = 0;

    /**
     * 租户ID
     */
    @TableField("tenant_id")
    @Schema(description = "租户ID", example = "1")
    private Long tenantId;

    /**
     * 备注
     */
    @Size(max = 500, message = "备注长度不能超过500")
    @TableField("remark")
    @Schema(description = "备注", example = "系统管理员")
    private String remark;

    // =========================== 便利方法 ===========================

    /**
     * 获取用户状态枚举
     * @return 用户状态枚举
     */
    public UserStatus getUserStatus() {
        return UserStatus.fromCode(this.status);
    }

    /**
     * 设置用户状态
     * @param userStatus 用户状态枚举
     */
    public void setUserStatus(UserStatus userStatus) {
        this.status = userStatus != null ? userStatus.getCode() : null;
    }

    /**
     * 检查用户是否激活
     * @return 是否激活
     */
    public boolean isActive() {
        UserStatus userStatus = getUserStatus();
        return userStatus != null && userStatus.isActive();
    }

    /**
     * 检查用户是否被禁用
     * @return 是否被禁用
     */
    public boolean isDisabled() {
        UserStatus userStatus = getUserStatus();
        return userStatus != null && userStatus.isDisabled();
    }

    /**
     * 增加登录次数
     */
    public void incrementLoginCount() {
        this.loginCount = (this.loginCount == null ? 0 : this.loginCount) + 1;
    }

    /**
     * 更新最后登录信息
     * @param loginTime 登录时间
     * @param loginIp 登录IP
     */
    public void updateLastLoginInfo(LocalDateTime loginTime, String loginIp) {
        this.lastLoginTime = loginTime;
        this.lastLoginIp = loginIp;
        incrementLoginCount();
    }

    // ===================== 手写的Getter方法 =====================
    
    public String getUsername() { return username; }
    public String getPassword() { return password; }
    public String getNickname() { return nickname; }
    public String getRealName() { return realName; }
    public String getEmail() { return email; }
    public String getPhone() { return phone; }
    public String getAvatar() { return avatar; }
    public Integer getStatus() { return status; }
    public Long getDeptId() { return deptId; }
    public Long getPositionId() { return positionId; }
    public LocalDateTime getLastLoginTime() { return lastLoginTime; }
    public String getLastLoginIp() { return lastLoginIp; }
    public Integer getLoginCount() { return loginCount; }
    public Long getTenantId() { return tenantId; }
    public String getRemark() { return remark; }

    // ==================== 手写的Setter方法 =====================
    
    public void setUsername(String username) { this.username = username; }
    public void setPassword(String password) { this.password = password; }
    public void setNickname(String nickname) { this.nickname = nickname; }
    public void setRealName(String realName) { this.realName = realName; }
    public void setEmail(String email) { this.email = email; }
    public void setPhone(String phone) { this.phone = phone; }
    public void setAvatar(String avatar) { this.avatar = avatar; }
    public void setStatus(Integer status) { this.status = status; }
    public void setDeptId(Long deptId) { this.deptId = deptId; }
    public void setPositionId(Long positionId) { this.positionId = positionId; }
    public void setLoginCount(Integer loginCount) { this.loginCount = loginCount; }
    public void setTenantId(Long tenantId) { this.tenantId = tenantId; }
    public void setRemark(String remark) { this.remark = remark; }
    public void setLastLoginTime(java.time.LocalDateTime lastLoginTime) { this.lastLoginTime = lastLoginTime; }
    public void setLastLoginIp(String lastLoginIp) { this.lastLoginIp = lastLoginIp; }
}
